package com.ponhu.ea2.goodsCenter.provider.utils;

import java.time.LocalDate;
import java.util.Random;

public class RandomUtil {

    /**
     * 生成编码 （规则：是否自营（1固定）+商品类型（01固定）+年月日+随机六位数）
     *
     * @return
     */
    public static String createGoodsCode() {
        StringBuilder goodsCode = new StringBuilder();
        String nowDate = LocalDateUtil.localDateFormat(LocalDate.now(), LocalDateUtil.FORMAT_PATTERN4);
        goodsCode.append("1");
        goodsCode.append("01");
        goodsCode.append(nowDate);
        goodsCode.append(getRandom(6));
        return goodsCode.toString();
    }

    /**
     * 产生指定长度的随机数
     *
     * @param length
     * @return
     */
    public static String getRandom(int length) {
        Random random = new Random();
        String fourRandom = random.nextInt(1000000) + "";
        int randLength = fourRandom.length();
        if (randLength < length) {
            for (int i = 1; i <= length - randLength; i++) {
                fourRandom = "0" + fourRandom;
            }
        }
        return fourRandom;
    }
}
